package com.lh.recallbootrabbitmq.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lh
 * @Description: rabbitmq配置类
 * @date 2021/8/17 11:42
 */
@Configuration
public class RabbitMQConfig {
	/**
	 * rabbitMQ服务器的地址
	 */
	@Value("${spring.rabbitmq.addresses}")
	private String addresses;
	/**
	 * rabbitMQ用户名
	 */
	@Value("${spring.rabbitmq.username}")
	private String username;
	/**
	 * rabbitMQ密码
	 */
	@Value("${spring.rabbitmq.password}")
	private String password;
	/**
	 * rabbitMQ虚拟机
	 */
	@Value("${spring.rabbitmq.virtual-host}")
	private String virtualHost;

	/**
	 * 注册rabbitMQ的Connection
	 *
	 * @return
	 */
	@Bean
	public ConnectionFactory connectionFactory() {
		CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
		cachingConnectionFactory.setAddresses(this.addresses);
		cachingConnectionFactory.setUsername(this.username);
		cachingConnectionFactory.setPassword(this.password);
		cachingConnectionFactory.setVirtualHost(this.virtualHost);
		return cachingConnectionFactory;
	}

	/**
	 * 注册rabbitAdmin 方便管理
	 *
	 * @param connectionFactory
	 * @return
	 */
	@Bean
	public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
		return new RabbitAdmin(connectionFactory);
	}

}
